package myicesscheduler.hibernate;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.hibernate.Session;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateUtilTest
{

   @BeforeClass
   static public void beforeClass()
   {
      // We need to add a class path manually to pass test cases on Ant
      final String curDir = System.getProperty( "user.dir" );
      final String sep = String.valueOf( File.pathSeparatorChar ); // this distinguishes paths. "/a/a.txt:/b/b.txt"
      final String delim = String.valueOf( File.separatorChar ); // this distinguishes folders. "/a/a.txt"

      // cf:
      // catalina.base/webapps/appname   /WEB-INF/classes : on tomcat
      // .(current directory) /WebContent/WEB-INF/classes : on ant
      final String classesPath = curDir + delim + "WebContent" + delim + "WEB-INF" + delim
            + "classes";
      final String dummyClassPaths = delim + sep + classesPath + sep
            + System.getProperty( "java.class.path", "." );
      System.setProperty( "java.class.path", dummyClassPaths );
   }

   @Test
   public void testFindDatabase() throws IOException, SecurityException, NoSuchMethodException,
         IllegalArgumentException, IllegalAccessException, InvocationTargetException
   {

      System
            .setProperty(
                  "java.class.path",
                  "/opt/local/share/java/apache-ant/lib/ant-stylebook.jar:/opt/local/share/java/apache-ant/lib/ant-swing.jar:/opt/local/share/java/apache-ant/lib/ant-testutil.jar:/opt/local/share/java/apache-ant/lib/ant-trax.jar:/opt/local/share/java/apache-ant/lib/ant-weblogic.jar:/opt/local/share/java/apache-ant/lib/ant.jar:/opt/local/share/java/apache-ant/lib/xercesImpl.jar:/opt/local/share/java/apache-ant/lib/xml-apis.jar:/someplace/tomcat/webapps/proj4398/WEB-INF/classes" );

      final Method findCatalinaBase = HibernateUtil.class.getDeclaredMethod( "findCatalinaBase" );
      assertNotNull( findCatalinaBase );
      findCatalinaBase.setAccessible( true );
      @SuppressWarnings( "unused" )
      final String catalBase = (String) findCatalinaBase.invoke( null );
      //assertNotNull(catalBase);
      //assertEquals("/someplace/tomcat", catalBase);
   }

   @Test
   public void GetUniqueId()
   {
      final Integer rst[] = new Integer[] { null };
      HibernateUtil.doTransaction( new IHibernateTransaction()
      {
         public void transaction( Session session )
         {
            rst[ 0 ] = HibernateUtil.getUniqueId( session, "LogUpdateTable", "internalId" );
         }
      } );

      assertEquals( 0, (int) rst[ 0 ] );
   }
   
   @Test
   public void ReplaceString()
   {
      final String org = "${catalina.base}/11";
      final String replaced = org.replaceFirst( "\\$\\{catalina.base\\}", "11" );
      assertEquals( "11/11", replaced );
   }

   static public void restart()
   {
      HibernateUtil.restart();
   }
}
